﻿$(document).ready(function(){
	// 菜单收缩显示
	$('li.FoldersList').live('click',function(){
		//if($('li.FoldersList').length > 3){
			//$('.ulSysFolders').hide();
			//$($(this).attr('show')).show();
		//}

		if($(this).is(".show")){
			$(this).removeClass("show");
			$($(this).attr('show')).hide();
		}else{
			$(this).addClass("show");
			$($(this).attr('show')).show();
		}
	});
	
	// 设定菜单是否展开
	if($('div.ulSysFolders').length <= 4){
		$('.ulSysFolders').show();
	}else{
		$('.ulSysFolders').hide();
		$('.ulSysFolders').eq(0).show();
	}
	
	// 菜单选中样式
	$('li.sysFolderClass').hover(
		function(){$(this).addClass('on');},
		function(){$(this).removeClass('on');}
	);
	
	$('li.sysFolderClass').live('click',function() {
		$(this).find('a#targets').click();
	});
	
	// 点击菜单
	$('a#targets').live('click',function() {
		var code = $(this).attr('code');
		var href = $(this).attr('href');
		var title  = $(this).find('span').text();
		var allobj = $('#divTab div');
		var divobj = $('#tabItem_'+code);
		
		// 创建选项卡
		var commentFn = {
			getTemplate : function(info){
			return '<div id="tabItem_'+info.code+'" class="Selected">\
						<ul>\
							<li id="divTab01"><a target="iframe_right" href="'+info.href+'" /></li>\
							<li id="divTab02" style="width:'+info.width+'"><a class="gTabfont" target="iframe_right" href="'+info.href+'">'+info.title+'</a></li>\
							<li id="divTab03"><img class="tabClose" title="关闭" src="' + JS.img + '/clear01.gif"></li>\
							<li id="divTab04"><a target="iframe_right" href="'+info.href+'" /></li>\
						</ul>\
					</div>';
			}
		}

		//判断当前选项卡对象是否存在
		if(divobj.length>0){
			allobj.removeClass('Selected').addClass('notSelected');
			divobj.removeClass('notSelected').addClass('Selected');
			$('#StartLoad').show();
			iframe_right.location.href = href;
			return false;
		}else{
			//if(allobj.size()>6){
				//$.dooDialog.alert("您打开的窗口太多了...");
				//return false;
			//}else{
				//移除选中选项卡（选中效果）
				allobj.removeClass('Selected').addClass('notSelected');
                var width = (($(document.body).width()-350)/6 - 32)+"px";
                if(allobj.size()>6){
                	width = (($(document.body).width()-350)/allobj.size() - 32)+"px";
                }

				$('#divTab').append(commentFn.getTemplate({
					code : code,
					href : href,
					title : title,
					width : width
				}));
                $('#divTab div li#divTab02').not('#divTab #tabItem_Wlcome li#divTab02').width(width);
			//}
		}

		return true;
	});
	
	// 选项卡选中关闭图标
	$('img.tabClose').live('mouseout',function(){
		$(this).attr('src',JS.img+'clear01.gif');
		return true;
	});

	$('img.tabClose').live('mouseover',function() {
		$(this).attr('src',JS.img+'clear02.gif');
		return true;
	});

	// 选中选项卡
	$('#divTab #divTab01,.gTabfont,#divTab04').live('click',function(){
		$('#StartLoad').show();
		$('#divTab div').removeClass('Selected').addClass('notSelected');
		$(this).parents('div').removeClass('notSelected').addClass('Selected');
		return true;
	});

	// 点击关闭选项卡
	$('#divTab #divTab03').live('click',function(){
		var divobj = $(this).parents('div');
		var selected = divobj.find('.Selected');

		if (divobj.attr('id') == selected.attr('id')){
			if (divobj.next('div').length>0){
				divobj.next('div').removeClass('notSelected').addClass('Selected');
				divobj.remove('#'+divobj.attr('id'));
			}else{
				divobj.prev('div').removeClass('notSelected').addClass('Selected');
				divobj.remove('#'+divobj.attr('id'));
			}

			$('#StartLoad').show();
			iframe_right.location.href = $('.mailTitle .Selected #divTab01 a').attr("href");
		}else{
			divobj.remove('#'+divobj.attr('id'));
		}		
		return true;
	});

	// 加载初始画面
	$('#StartLoad').show();
	iframe_right.location.href = "/welcome";

	// 画面大小调整
	$(window).resize(function(){
		//var iheight = $(window).height()-165;
		var iheight = $(window).height()-92;
		var iwidth  = $(window).width()-191;
		$('#iframe_right').height(iheight);
		$('#iframe_right').width(iwidth);
		$('div#Folders').height(iheight-35);
		iframe_right.document.getElementsByTagName("body").height = iheight;
		$('.gMain tr').eq(2).height(iheight);
	});
});